package sad.visao;

import java.awt.Color;
import javax.swing.table.TableModel;
import sad.util.DAOEvent;

/**
 *
 * @author Junior
 */
public class ListProfDialog extends DialogAbstrato {

    /** Creates new form ListProdutoDialo */
    public ListProfDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        listaProdutos = new javax.swing.JPanel();
        scrProfessor = new javax.swing.JScrollPane();
        tblProf = new javax.swing.JTable();
        lblMessage = new javax.swing.JLabel();
        btnNovo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SAD - Consulta");

        listaProdutos.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista Professores"));
        listaProdutos.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        tblProf.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblProf.getTableHeader().setReorderingAllowed(false);
        scrProfessor.setViewportView(tblProf);

        lblMessage.setText("");

        javax.swing.GroupLayout listaProdutosLayout = new javax.swing.GroupLayout(listaProdutos);
        listaProdutos.setLayout(listaProdutosLayout);
        listaProdutosLayout.setHorizontalGroup(
            listaProdutosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(listaProdutosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(listaProdutosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrProfessor, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
                    .addComponent(lblMessage, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE))
                .addContainerGap())
        );
        listaProdutosLayout.setVerticalGroup(
            listaProdutosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(listaProdutosLayout.createSequentialGroup()
                .addComponent(scrProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(lblMessage)
                .addContainerGap())
        );

        btnNovo.setText("Novo");
        btnNovo.setActionCommand("Novo Relatorio");

        btnEditar.setText("Editar");

        btnExcluir.setText("Excluir");

        btnCancelar.setText("Cancelar");
        btnCancelar.setActionCommand("Cancelar Relatorio");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listaProdutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                    .addComponent(btnExcluir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(listaProdutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JPanel listaProdutos;
    private javax.swing.JScrollPane scrProfessor;
    private javax.swing.JTable tblProf;
    // End of variables declaration//GEN-END:variables

    // <editor-fold defaultstate="collapsed" desc="Responde a eventos DAO">
    @Override
    public void modelDAOChange(DAOEvent evt) {
        if(evt.getID()==DAOEvent.DAO_UPDATE){
            lblMessage.setForeground(Color.green);
            lblMessage.setText("Professor atualizado com sucesso!!!");
        }
        if(evt.getID()==DAOEvent.DAO_DELETE){
            lblMessage.setForeground(Color.green);
            lblMessage.setText("Professor excluído com sucesso!!!");
        }
        if(evt.getID()==DAOEvent.DAO_CREATE){
          if(tblProf.getModel().getRowCount()!=0){
                int rowCount = tblProf.getRowCount();
              tblProf.setRowSelectionInterval(rowCount-1,rowCount-1);
          }
        }
        if(evt.getID()==DAOEvent.DAO_ERROFIELD){
            lblMessage.setForeground(Color.red);
            lblMessage.setText("Você deve selecionar um "+evt.getField()+"!!!");
        }
    }// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Retorna objeto selecionado">    
    public Object[] getSelectedProfessor(){
        Object[] dados={null,null,null,null};
        //só envia dados se tem uma linha selecionada, senão null
        if(tblProf.getSelectedRow()!=-1){
            for (int i=0;i< tblProf.getModel().getColumnCount();i++){
            dados[i] = tblProf.getModel().getValueAt(tblProf.getSelectedRow(), i);
            }
            dados[3]= tblProf.getSelectedRow();
        }
        return dados;
    }//</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Métodos públicos comuns a todos os Dialogs">
    @Override
    public void addOuvintes() {
        btnNovo.addActionListener(getSuporteEventos());
        btnEditar.addActionListener(getSuporteEventos());
        btnExcluir.addActionListener(getSuporteEventos());
        btnCancelar.addActionListener(getSuporteEventos());
    }

    @Override
    public void close() {
        this.resetListProf();
        this.dispose();
    }

    @Override
    public void setEditableDados(Object[] d) {
    }//</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc="Métodos públicos comuns a todos os relatorios">
    public void setTblProf(TableModel m) {
        tblProf.setModel(m);
        //Toda vez que abre a Janela de Relatorio deixa o primeiro elemento 
        //selecionado
        if (m.getRowCount()!=0){
            tblProf.setRowSelectionInterval(0, 0);
        }
    }
    
    public void resetMessage() {
        lblMessage.setText(null);
    }
    
    public void setSelectedRow(int line) {
        tblProf.setRowSelectionInterval(line, line);
    }//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Métodos privados, mas comuns a todos os relatorios">
    private void resetListProf() {
        tblProf.setVisible(false);
    }//</editor-fold>
}